<?php

/**
 * This is the model class for table "tbl_vendors".
 *
 * The followings are the available columns in table 'tbl_vendors':
 * @property string $vendor
 * @property string $company
 * @property string $firstname
 * @property string $add1
 * @property string $add2
 * @property string $city
 * @property string $state
 * @property string $zip
 * @property string $lblphone1
 * @property string $lblphone2
 * @property string $lblphone3
 * @property string $lblphone4
 * @property string $phone1
 * @property string $phone2
 * @property string $phone3
 * @property string $phone4
 * @property string $terms
 * @property string $accountno
 * @property double $discount
 * @property string $post
 * @property string $rwvendist
 * @property string $rwvenacct
 * @property string $email
 * @property string $weburl
 * @property integer $print1099
 * @property string $taxid
 * @property string $imagepath
 */
class Vendors extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Vendors the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_vendors';
	}

       

        
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('vendor, company, add1, city, state, zip, terms, discount', 'required'),
			array('print1099', 'numerical', 'integerOnly'=>true),
			array('discount', 'numerical'),
			array('vendor', 'length', 'max'=>4),
			array('company, firstname, add1, add2, city, accountno, taxid', 'length', 'max'=>50),
			array('state', 'length', 'max'=>2),
			array('zip, rwvenacct', 'length', 'max'=>10),
			array('lblphone1, lblphone2, lblphone3, lblphone4', 'length', 'max'=>15),
			array('phone1, phone2, phone3, phone4', 'length', 'max'=>12),
			array('terms', 'length', 'max'=>31),
			array('post', 'length', 'max'=>1),
			array('rwvendist', 'length', 'max'=>6),
			array('email, imagepath', 'length', 'max'=>150),
			array('weburl', 'length', 'max'=>255),
                        array('vendor','unique','className'=>'Vendors','attributeName'=>'vendor','message'=>'Vendor Number already taken.  Please try a different one.'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('vendor, company, firstname, add1, add2, city, state, zip, lblphone1, lblphone2, lblphone3, lblphone4, phone1, phone2, phone3, phone4, terms, accountno, discount, post, rwvendist, rwvenacct, email, weburl, print1099, taxid, imagepath', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'vendor' => 'Vendor',
			'company' => 'Company/Lastname',
			'firstname' => 'Firstname',
			'add1' => 'Address 1',
			'add2' => 'Address 2',
			'city' => 'City',
			'state' => 'State',
			'zip' => 'Zip',
			'lblphone1' => 'Phone 1',
			'lblphone2' => 'Phone 2',
			'lblphone3' => 'Phone 3',
			'lblphone4' => 'Phone 4',
			'phone1' => 'Phone1',
			'phone2' => 'Phone2',
			'phone3' => 'Phone3',
			'phone4' => 'Phone4',
			'terms' => 'Terms',
			'accountno' => 'Account Number',
			'discount' => 'Discount',
			'post' => 'Post',
			'rwvendist' => 'Rwvendist',
			'rwvenacct' => 'Rwvenacct',
			'email' => 'E-mail',
			'weburl' => 'Web Address',
			'print1099' => 'Print 1099',
			'taxid' => 'Tax ID',
			'imagepath' => 'Image',
		);
	}
        public function getList()
        {
            $vendors = $this->findAll();
            $data = array();
            foreach($vendors as $vendor){
                $data[$vendor->vendor] = "$vendor->vendor - $vendor->company";
            }
            return $data;
        }
        public function afterSave() {
            
            if($this->isNewRecord){
                $counter=intval($this->vendor)+1;
                $counter=str_pad($counter, 4, '0', STR_PAD_LEFT);
                Counters::model()->saveNumber('vendor',$counter);
                $location=new Vendorlocations;
                $location->vendorno=$this->vendor;
                $location->vendorlocno='0001';
                $location->vendorlocname=$this->company;
                $location->vendorlocadd1=$this->add1;
                $location->vendorlocadd2=$this->add2;
                $location->vendorloccity=$this->city;
                $location->vendorlocstate=$this->state;
                $location->vendorloczip=$this->zip;
                $location->vendorlocinactive=0;
                $location->save();
            }
            return parent::afterSave();
        }
        
        public function afterDelete() {
            Vendorlocations::model()->deleteAll('vendorno=:vendorno',array('vendorno'=>$this->vendor));
            Vendornotes::model()->deleteAll('vendorno=:vendorno',array('vendorno'=>$this->vendor));
            Vendordocuments::model()->deleteAll('vendorno=:vendorno',array('vendorno'=>$this->vendor));
            Vendorcontacts::model()->deleteAll('vendorno=:vendorno',array('vendorno'=>$this->vendor));
            
            return parent::afterDelete();
        }
        
        public static function getText($id){
            return Yii::app()->db->createCommand('select company from tbl_vendors where id=:id')->queryScalar(array(
                'id'=>$id
            ));
        }
}